package my.jbf.firstGame.BITMAP;

import my.jbf.firstGame.GAMEDATA.*;

import android.graphics.*;
import android.view.*;
import my.jbf.firstGame.*;

public class DirectionKey
{
	private Canvas mCanvas;
	private SharedPreferenceManager mSPM;
	private Paint mPaint;
	private Bitmap mBitmap;
	private int OutCircleSize;
	
	public DirectionKey()
	{
		mSPM = MainActivity.mSPM;
		mMoveContral = new PointF(0f, 0f);
		mTouchDownPoint = new PointF();
		mTouchDownPoint.set((float)(mSPM.getDirectionKeySize() / 2), (float)(mSPM.getDirectionKeySize() / 2));
		mPaint = new Paint();
		mPaint.setColor(mSPM.getDirectionKeyColor());
		mPaint.setStyle(Paint.Style.STROKE);
		mBitmap = Bitmap.createBitmap(mSPM.getDirectionKeySize(), mSPM.getDirectionKeySize(), Bitmap.Config.ARGB_8888);
		mCanvas = new Canvas(mBitmap);
		OutCircleSize=mSPM.getDirectionKeySize()*2/3;
		reDraw();
	}

	/*左侧按钮设置*/
	private boolean isTouchDown=false;
	public PointF mTouchDownPoint;
	private int mTouchID=-1;
	private PointF mMoveContral;//给定外部数据用……不知道用什么名字比较合适

	/*左侧设置完毕*/










	private boolean isInDirectionKey(float InSideX, float InSideY)
	{
		//判断触点是否在方向键范围内

		if (InSideX >= 0 && InSideY >= 0 && InSideX <= mSPM.getDirectionKeySize()&& InSideY <= mSPM.getDirectionKeySize())
		{
			return (Math.pow((InSideX -  (int)((float)(mSPM.getDirectionKeySize() / 2))), 2) + Math.pow((InSideY -  (int)((float)(mSPM.getDirectionKeySize() / 2))), 2) <= Math.pow( (int)((float)(mSPM.getDirectionKeySize() / 2)), 2));
		}
		return false;
	}

	public boolean onTouchEvent(MotionEvent event)
	{
		switch (event.getActionMasked())
		{
			case MotionEvent.ACTION_DOWN:
				{
//					if (isInDirectionKey(event.getX() - msp.getDirectionKeyXY.x, event.getY() - msp.getDirectionKeyXY.y))
//					{
//						isTouchDown = true;
//						setLeftButtonDown(event.getX() - msp.getDirectionKeyXY.x, event.getY() - msp.getDirectionKeyXY.y);
//						mTouchID = event.getPointerId(event.getActionIndex());
//					}
//					break;
				}
			case MotionEvent.ACTION_POINTER_DOWN:
				{

					if (isInDirectionKey(event.getX(event.getActionIndex()) - mSPM.getDirectionKeyXY().x, event.getY(event.getActionIndex()) - mSPM.getDirectionKeyXY().y))
					{
						isTouchDown = true;
						setLeftButtonDown(event.getX(event.getActionIndex()) - mSPM.getDirectionKeyXY().x, event.getY(event.getActionIndex()) - mSPM.getDirectionKeyXY().y);
						mTouchID = event.getPointerId(event.getActionIndex());
					}
					break;


				}

			case MotionEvent.ACTION_MOVE:
				{
					if (isTouchDown)
					{
						setLeftButtonDown(event.getX(event.findPointerIndex(mTouchID)) - mSPM.getDirectionKeyXY().x, event.getY(event.findPointerIndex(mTouchID)) - mSPM.getDirectionKeyXY().y);
					}
					break;
				}
			case MotionEvent.ACTION_POINTER_UP:
				{
					if (event.getPointerId(event.getActionIndex()) == mTouchID)
					{
						isTouchDown = false;
						mTouchID = -1;
						mTouchDownPoint.set(OutCircleSize * 3 / 2, OutCircleSize * 3 / 2);
					}
					break;
				}
			case MotionEvent.ACTION_UP:
				{
					isTouchDown = false;
					mTouchID = -1;
					mTouchDownPoint.set(mSPM.getDirectionKeySize()/2 , mSPM.getDirectionKeySize()/2 );
					break;
				}
			case MotionEvent.ACTION_CANCEL:
				{

				}
		}
		return true;
	}
	public PointF getMoveContral()
	{
		float x=(mTouchDownPoint.x - mSPM.getDirectionKeyXY().x) / OutCircleSize;
		float y=(mTouchDownPoint.y - mSPM.getDirectionKeyXY().y) / OutCircleSize;
		mMoveContral.set(x, y);
		return mMoveContral;
	}
	public Bitmap getbitmap()
	{
		reDraw();
		return mBitmap;
	}
	private void reDraw()
	{
		mBitmap.eraseColor(Color.argb(0, 0, 0, 0));
		mCanvas.drawCircle(mSPM.getDirectionKeySize() / 2, mSPM.getDirectionKeySize() / 2, OutCircleSize / 2, mPaint);
		mCanvas.drawCircle(mTouchDownPoint.x, mTouchDownPoint.y, OutCircleSize / 4, mPaint);
		mCanvas.drawText(mTouchDownPoint.x+","+mTouchDownPoint.y,50,50,mPaint);
	}
	private void setLeftButtonDown(float xf, float yf)
	{
		if (Math.pow((xf - (float)mSPM.getDirectionKeySize()/2), 2)
			+ Math.pow((yf - (float)mSPM.getDirectionKeySize()/2), 2)
			<= Math.pow((float)(OutCircleSize/2), 2))
		{
			mTouchDownPoint.set((int)xf, (int)yf);
			
		}
		else
		{
			if (xf == (int)((float)(mSPM.getDirectionKeySize()/2)) )
			{
				if (yf >  (int)((float)(mSPM.getDirectionKeySize()/2 )))
				{
					mTouchDownPoint.set(mSPM.getDirectionKeySize()/2 , OutCircleSize/2+mSPM.getDirectionKeySize()/2);
				}
				else
				{
					mTouchDownPoint.set(mSPM.getDirectionKeySize()/2, 0);
				}
			}
			else if (yf == OutCircleSize )
			{
				if (xf >  (int)((float)(OutCircleSize * 3 / 2)))
				{
					mTouchDownPoint.set(OutCircleSize/2+mSPM.getDirectionKeySize()/2, mSPM.getDirectionKeySize() / 2);
				}
				else
				{
					mTouchDownPoint.set(0, mSPM.getDirectionKeySize() / 2);
				}
			}
			else 
			{
				double ddx=Math.sqrt(Math.pow(OutCircleSize/2, 2) / (1 + Math.pow((yf -  (int)((float)(mSPM.getDirectionKeySize()/2))) / (xf -  (int)((float)(mSPM.getDirectionKeySize()/2))), 2)));
				double x;
				if (xf > (int)((float)( mSPM.getDirectionKeySize()/2)))
				{
					x =  (int)((float)(mSPM.getDirectionKeySize()/2)) + ddx;
				}
				else
				{
					x =  (int)((float)(mSPM.getDirectionKeySize()/2)) - ddx;
				}
				double y= (int)((float)(mSPM.getDirectionKeySize()/2)) + (yf - mSPM.getDirectionKeySize()/2) * (x - mSPM.getDirectionKeySize()/2) / (xf - mSPM.getDirectionKeySize()/2);
				mTouchDownPoint.set((int)x, (int)y);
			}


		}


	}



}
